// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/ast/ddl.h>
#include <cantor/parser/ast/stmt.h>
#include <cantor/parser/ast/clause.h>

namespace cantor::parser {

    nlohmann::ordered_json DmlNode::to_json_impl() const {
        auto j = DefiniteExpr::to_json_impl();
        return j;
    }
    nlohmann::ordered_json Assignment::to_json() const {
        nlohmann::ordered_json j = DmlNode::to_json_impl();
        if (name) {
            j["assign_name"] = name->to_json();
        } else {
            j["assign_name"] = nlohmann::ordered_json();
        }
        if (expr) {
            j["expr"] = expr->to_json();
        } else {
            j["expr"] = nlohmann::ordered_json();
        }
        return j;
    }
    nlohmann::ordered_json FieldItem::to_json() const {
        nlohmann::ordered_json j = DmlNode::to_json_impl();
        j["type"] = type;
        j["value"] = value.to_string();
        j["opt_enclosed"] = opt_enclosed;
        return j;
    }
    nlohmann::ordered_json LinesClause::to_json() const {
        nlohmann::ordered_json j = DmlNode::to_json_impl();

        j["starting"] = starting.to_string();
        j["terminated"] = terminated.to_string();
        return j;
    }
    nlohmann::ordered_json FieldsClause::to_json() const {
        nlohmann::ordered_json j = DmlNode::to_json_impl();
        j["terminated"] = terminated.to_string();
        j["enclosed"] = enclosed.to_string();
        j["escaped"] = escaped.to_string();
        j["opt_enclosed"] = opt_enclosed;
        return j;
    }

    void Assignment::to_stream(std::ostream &os) const {
        name->to_stream(os);
        os <<" = " << expr;
    }
}  // namespace cantor::parser
